<template>
  <div class="loginwrapper">
    <div class="login-form">
      <div class="flexbetween">
        <img src="@/assets/images/logo.png" alt="" class="logo" />
        <div class="f-14 c_primary commhover" @click="handlePage('/register')">免费注册</div>
      </div>
      <div class="login_body">
        <div class="flexcenter login_type mb-20">
            <div class="textcenter f-18 commhover" :class="{c_primary:active==1}" @click="handleSelect(1)">微信登录</div>
            <div class="textcenter f-18 commhover" :class="{c_primary:active==2}" @click="handleSelect(2)">账号登录</div>
        </div>
        <WxcodeLogin v-if="active==1" />
        <AccountLogin v-if="active==2" />
      </div>
    </div>
    <!--  底部  -->
    <div class="el-login-footer mt-20">
      <span class="c_999 f-14">Copyright © 2023-现在 湖南锐行科技有限公司 All Rights Reserved 湘ICP备 14031293号-1</span>
    </div>
  </div>
</template>

<script>
import WxcodeLogin from './WxcodeLogin.vue';
import AccountLogin from './AccountLogin.vue';
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      active:1,
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  components:{
    WxcodeLogin,
    AccountLogin
  },
  created() {
    // this.getCookie();
  },
  methods: {

    handleSelect(type){
      this.active = type
    },

    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    },

    handlePage(path){
      this.$router.push({path})
    }
  }
};
</script>

<style lang="scss" scoped>
@import '@/assets/styles/login.scss'
// .login {
//   display: flex;
//   flex-direction: column;
//   justify-content: center;
//   align-items: center;
//   height: 100%;
//   background-color: #f1f1f1;
// }
// .logo{
//   height: 50px;
// }
// .login_body{
//   padding-top: 54px;
//   width: 328px;
//   margin: 0 auto;
//   min-height: 495px;
// }
// .login_type{
//   &>div{
//     flex: 1;
//   }
//   &>div:first-child{
//     border-right: 1px solid #e9e9e9;
//   }
// }
// .login-form {
//   border-radius: 6px;
//   background: #ffffff;
//   width: 798px;
//   padding: 35px 36px;
//   box-shadow: 0 0 20px 0 rgba(0,0,0,.05);
//   border: 1px solid #d8d8d8;
// }
// .login-tip {
//   font-size: 13px;
//   text-align: center;
//   color: #bfbfbf;
// }
// .el-login-footer {
//   height: 40px;
//   line-height: 40px;
//   width: 100%;
//   text-align: center;
//   color: #fff;
//   font-family: Arial;
//   font-size: 12px;
//   letter-spacing: 1px;
// }
// .login-code-img {
//   height: 38px;
// }
</style>
